package com.group6.exam.student.dao;

import java.util.List;
import java.util.Map;

import com.group6.exam.common.util.CollectionUtil;
import com.group6.exam.common.util.DBUtil;
import com.group6.exam.student.entity.StudentEntity;

public class StudentDaoImpl implements IStudentDao {

	/**
	 * 
	 * @see com.group6.exam.student.dao.IStudentDao#login(com.group6.exam.student
	 *      .entity.StudentEntity)
	 */
	public boolean login(StudentEntity student) {

		String sql = "SELECT 1 FROM studentInfo WHERE student_sno=? AND student_password=?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql,
				student.getAccount(), student.getPassword());
		System.out.println(student.getAccount());
		return CollectionUtil.isEmpty(list);
	}

	/**
	 * 查询主菜单
	 */
	public List<Map<String, Object>> getMainMenus(String studentno) {
		String sql = "select * from rightInfo where right_id "
				+ "in (select right_id from rolerightinfo where role_id=(select role_id from studentinfo where student_sno=?)) and parent_id='root'";
		return DBUtil.executeQuery(sql, studentno);
	}

	/**
	 * 查询子菜单
	 */
	public List<Map<String, Object>> getChildMenus(String studentno) {
		String sql = "select * from rightInfo where right_id "
				+ "in (select right_id from rolerightinfo where role_id=(select role_id from studentinfo where student_sno=?)) and parent_id <>'root'";
		return DBUtil.executeQuery(sql, studentno);
	}

	public List<Map<String, Object>> search(String studentno) {
		String sql = "select * from studentinfo where  student_sno=?";
		return DBUtil.executeQuery(sql, studentno);

	}

	/**
	 * 修改密码
	 * 
	 * @see com.group6.exam.student.dao.IStudentDao#update(java.lang.String,
	 *      java.lang.String)
	 */
	@Override
	public int update(String studentsno, String newpassword) {
		String sql = "update studentinfo set student_password=? where student_sno=?";
		return DBUtil.executeUpdate(sql, newpassword, studentsno);
	}

	/**
	 * 修改电话
	 */
	@Override
	public int updatephone(String stuno, String stuphone) {

		String sql = "update studentinfo set student_phone=? where student_sno=?";
		return DBUtil.executeUpdate(sql, stuphone, stuno);
	}

	/**
	 * 查詢分數
	 */
	@Override
	public List<Map<String, Object>> score(String stuno) {
		String sql = "SELECT score_ispass,student_sno,student_name,score_score,subject_name "
				+ "FROM studentinfo stu, score sc, `subject` sub "
				+ "WHERE  stu.student_id=sc.score_id AND sc.score_id=sub.subject_id AND stu.student_id=sub.subject_id AND student_sno=?";
		return DBUtil.executeQuery(sql, stuno);
	}

	@Override
	public List<Map<String, Object>> start(String stuno) {

		String sql = "SELECT student_sno,student_name,paper_name,exam_examtime,exam_time,paper_id "
				+ "FROM studentinfo stu, examarrange ex, paper p "
				+ "WHERE stu.grade_id=ex.exam_grade_id AND ex.exam_paper_id=p.paper_id AND stu.student_sno=?";
		return DBUtil.executeQuery(sql, stuno);
	}
}
